package com.kylinhunter.ch.plugin.knowledgebase.ui;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.kylinhunter.ch.language.ResourceManager;
import com.kylinhunter.ch.main.CHMainUI;
import com.kylinhunter.ch.plugin.knowledgebase.service.KBService;
import com.kylinhunter.exception.KHException;
import com.kylinhunter.exception.KHExceptionC;
import com.kylinhunter.ui.common.ResourceManagerAware;
import com.kylinhunter.ui.common.action.CommonAction;
 

public class AddFileWindowAction   extends CommonAction implements ResourceManagerAware  {
	KBService kbservice = null;
	public static final int Action_AddFile=0;
	public static final int Action_Init=1;
	private String writeFileError="";
	private String readFileError="";
	private String saveOK="";
	private ResourceManager resourceManager = null;
	private static final Log log = LogFactory.getLog(AddFileWindowAction.class);
 
	public void addFile(AddFileWindow addFileWindow) throws KHException{

		try {
			String fileContent = addFileWindow.getEditorPane().getText();
			String fileId=addFileWindow.getWindows_id();
			File file=new File(this.kbservice.getFilePath(fileId)+File.separator+fileId+".txt");
			log.debug("file path:"+file.getAbsolutePath());			
			PrintWriter pw=null;
			try {
				if(!file.getParentFile().exists())file.getParentFile().mkdirs();
				if(!file.exists())file.createNewFile();
				pw=new PrintWriter(file,"unicode");
				pw.println(fileContent);
				//throw new KHException (KHExceptionC.ERROR,"","AddFileWindowDo",writeFileError);
				this.getDialog().showInformationMessage(addFileWindow, this.saveOK);
			
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw new KHException (KHExceptionC.ERROR,"","AddFileWindowDo",writeFileError);
			}finally{
				pw.close();
			}


		} catch (KHException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		}


	}
	public String getFile(AddFileWindow addFileWindow) throws KHException{

		byte[] data=null;
		
		File file=this.kbservice.getFileByFileID(addFileWindow.getWindows_id());
		log.debug("file path:"+file.getAbsolutePath());		
		if(!file.exists())return "";
		BufferedInputStream reader=null;
		try {
			
			reader=new BufferedInputStream(new FileInputStream(file));
			data=new byte[reader.available()];
			reader.read(data); 

			data.toString();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new KHException (KHExceptionC.ERROR,"","AddFileWindowDo",readFileError);
		}finally{
			try {
				reader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		//log.debug("get file content:"+data.toString());
		try {
			return new String(data,"unicode");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	public KBService getKbservice() {
		return kbservice;
	}

	public void setKbservice(KBService kbservice) {
		this.kbservice = kbservice;
	}
	public void init(AddFileWindow addFileWindow) throws KHException {
		// TODO Auto-generated method stub			
			addFileWindow.getEditorPane().setText(this.getFile(addFileWindow));		
			addFileWindow.updateResource(resourceManager);
		
	}
	@Override
	public void  updateResource(ResourceManager resourceManager) {
		// TODO Auto-generated method stub
		this.readFileError=resourceManager.getString(KownledgeBasePlugin.RESOURCE,"addFileWindowDo.readFileError");
		this.writeFileError=resourceManager.getString(KownledgeBasePlugin.RESOURCE,"addFileWindowDo.writeFileError");
		this.saveOK=resourceManager.getString(KownledgeBasePlugin.RESOURCE,"addFileWindowDo.saveOK");
		
	}
	@Override
	public void execute(Object parent,Object... param) throws Exception {
		int p=((Integer)param[0]).intValue();
		AddFileWindow pa=(AddFileWindow)parent;
		if(p==this.Action_AddFile){
			this.addFile(pa);
		}
	
		
		
		
		if(p==this.Action_Init){
			this.init(pa);
		}
	}
	@Override
	public void setResourceManager(ResourceManager resourceManager) {
		// TODO Auto-generated method stub
		this.resourceManager=resourceManager;
	} 
}
